package com.ruibo.tax.mq.config;


import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;


import com.ruibo.tax.constants.TaxConstants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/14 14:10
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "ibm")
public class IbmMqConfig {

    private String host;

    private String channel;

    private Integer port;

    private String queueManagerName;

    private int ccsid;


    private long receiveTimeout = 20000L;

    @Bean
    MQQueueManager mqQueueManager() {
        MQQueueManager queueManager = null;
        MQEnvironment.hostname = host;
        MQEnvironment.port = port;
        MQEnvironment.channel = channel;
        MQEnvironment.CCSID = ccsid;
        //MQ中拥有权限的用户名
        // MQEnvironment.userID = "MUSR_MQADMIN";
        //用户名对应的密码
        // MQEnvironment.password = "123456";

        try {
            queueManager = new MQQueueManager(queueManagerName);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            log.error("创建队列管理器失败");
            System.exit(-1);
        }

        return queueManager;
    }


    @Bean("QLHB2SW")
    MQQueue QLHB2SW(MQQueueManager queueManager) {
        MQQueue putQueue = null;
        try {
            int openOptionsArg = 16 | 1 | 32;
            putQueue = queueManager.accessQueue(TaxConstants.QLHB2SW, openOptionsArg);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("连接队列失败");
            System.exit(-1);
        }
        return putQueue;
    }

    @Bean("QLSW2HB")
    MQQueue QLSW2HB(MQQueueManager queueManager) {
        MQQueue putQueue = null;
        try {
            int openOptionsArg = 16 | 1 | 32;
            putQueue = queueManager.accessQueue(TaxConstants.QLSW2HB, openOptionsArg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("连接队列失败");
            log.error(e.toString());
            System.exit(-1);
        }
        return putQueue;
    }

    @Bean("DZ_QLHB2SW")
    MQQueue DZ_QLHB2SW(MQQueueManager queueManager) {
        MQQueue putQueue = null;
        try {
            int openOptionsArg = 16 | 1 | 32;
            putQueue = queueManager.accessQueue(TaxConstants.DZ_QLHB2SW, openOptionsArg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            log.error("连接队列失败");
            System.exit(-1);
        }
        return putQueue;
    }

    @Bean("DZ_QLSW2HB")
    MQQueue DZ_QLSW2HB(MQQueueManager queueManager) {
        MQQueue putQueue = null;
        try {
            int openOptionsArg = 16 | 1 | 32;
            putQueue = queueManager.accessQueue(TaxConstants.DZ_QLSW2HB, openOptionsArg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            log.error("连接队列失败");
            System.exit(-1);
        }
        return putQueue;
    }


//    @Bean
//    public MQQueueConnectionFactory mqQueueConnectionFactory() {
//        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
//        mqQueueConnectionFactory.setHostName(host);
//        try {
//            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
//            mqQueueConnectionFactory.setCCSID(ccsid);
//            mqQueueConnectionFactory.setChannel(channel);
//            mqQueueConnectionFactory.setPort(port);
//            mqQueueConnectionFactory.setQueueManager(queueManagerName);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return mqQueueConnectionFactory;
//    }
//
//
//    @Bean
//    @Primary
//    public CachingConnectionFactory cachingConnectionFactory(MQQueueConnectionFactory mqQueueConnectionFactory) {
//        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
//        cachingConnectionFactory.setTargetConnectionFactory(mqQueueConnectionFactory);
//        cachingConnectionFactory.setSessionCacheSize(500);
//        cachingConnectionFactory.setReconnectOnException(true);
//        return cachingConnectionFactory;
//    }
//
//
//    //如果只是获取消息，此处无需配置
//    @Bean
//    public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
//        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
//        jmsTemplate.setReceiveTimeout(receiveTimeout);
//        return jmsTemplate;
//    }
}
